const worker = new InlineWorker(() => { // START OF WORKER THREAD CODE console.log('Start worker thread, wait for postMessage: ');
const calculateCountOfPrimeNumbers = (limit) => {
const isPrime = num => { for (let i = 2; i < num; i++) { if (num % i === 0) { return false; } } return num > 1; };
let countPrimeNumbers = 0;
while (limit >= 0) { if (isPrime(limit)) { countPrimeNumbers += 1; } limit--; }
// this is from DedicatedWorkerGlobalScope ( because of that we have postMessage and onmessage methods ) // and it can't see methods of this class // @ts-ignore this.postMessage({ primeNumbers: countPrimeNumbers }); };
// @ts-ignore this.onmessage = (evt) => { console.log('Calculation started: ' + new Date()); calculateCountOfPrimeNumbers(evt.data.limit); }; // END OF WORKER THREAD CODE });